import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xiaomishop/app/net/HttManager.dart';

import '../../../data/good_model.dart';
import '../../../net/Url.dart';

class GoodlistController extends GetxController {
  //TODO: Implement GoodlistController

  final count = 0.obs;
  RxList<GoodModelItem> goodModelItem = <GoodModelItem>[].obs;
  final ScrollController scrollController = ScrollController();
  RxInt itemCount = 0.obs;
  int page = 1;
  int pageSize = 8;
  String? cid  = Get.arguments["id"];
  String? keyword  = Get.arguments["keyword"];
  RxString sort = "all".obs;

  RxBool isRefresh = false.obs;

  RxBool loadNoData = false.obs;

  GlobalKey<ScaffoldState> scaffoldGlobalKey = new GlobalKey<ScaffoldState>();

  @override
  void onInit() {
    super.onInit();
    initListener();
    refreshGoodList();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void increment() => count.value++;

  /// 下拉刷新
  void refreshGoodList() {
    loadNoData.value = false;
    isRefresh.value = true;
    page = 1;
    initGoodList();
  }

  /// 上拉加载
  void loadAddGoodList() {
    if (isGetDataing) {
      return;
    }
    page++;
    initGoodList();
  }

  bool isGetDataing = false;

  void setCurrentSort(String sortValue) {
    if (sortValue == "price") {
      if (sort.value == "price_1") {
        sort.value = "price_-1";
      } else if (sort.value == "price_-1") {
        sort.value = "price_1";
      } else {
        sort.value = "price_1";
      }
    } else if (sortValue == "salecount") {
      if (sort.value == "salecount_1") {
        sort.value = "salecount_-1";
      } else if (sort.value == "salecount_-1") {
        sort.value = "salecount_1";
      } else {
        sort.value = "salecount_1";
      }
    } else if (sortValue == "all") {
      sort.value = "all";
    }
    update();
    refreshGoodList();
  }

  RxString title = "筛选".obs;

  void initGoodList() async {
    if (isGetDataing) {
      return;
    }
    print('请求数据 ${keyword}');
    isGetDataing = true;
    Map<String, dynamic> map = { "page": page, "pageSize": pageSize};
    if (sort.value != "all" && sort.value.isNotEmpty) {
      map["sort"] = sort;
    }
    if(cid!=null){
      map["cid"] = cid;
    }
    if(keyword!=null){
      map["search"] = keyword;
    }

    var rsp = await HttManager.instance.get(Url.API_PLIST, map: map);
    isRefresh.value = false;
    // isLoad.value = false;
    if (rsp != null) {
      List<GoodModelItem> item = GoodModel.fromJson(rsp.data).result;
      print('请求结果 ${item.length}');
      if (item != null && item.length > 0) {
        if (page == 1) {
          goodModelItem.value.clear();
          goodModelItem.value = [];
          update();
        }

        goodModelItem.value.addAll(item);
        itemCount.value = goodModelItem.value.length + 1;
        goodModelItem.value = goodModelItem.value;
        if(itemCount.value<pageSize){
          loadNoData.value = true;
        }
        // goodModelItem.refresh();
        goodModelItem.value.forEach((element) {
          print('最后结果：${element.title}');
        });
        update();
        isGetDataing = false;
        // if (page == 1) {
        //   scrollController.animateTo(
        //     0,
        //     duration: Duration(milliseconds: 500),
        //     curve: Curves.ease,
        //   );
        // }
      } else {
        dealNoData();
      }
    } else {
      dealNoData();
    }
  }

  /// 处理没数据的情况
  void dealNoData() {
    print('dealNoData ${page}');
    if (page == 1) {
      goodModelItem.value = [];
    }
    if (page > 1) {
      page = page - 1;
      loadNoData.value = true;
    }
    isGetDataing = false;
    update();
  }

  void initListener() {
    //监听listview的滑动
    scrollController.addListener(() {
      var pixels = scrollController.position.pixels;
      var maxScrollExtent = scrollController.position.maxScrollExtent;
      if (maxScrollExtent - pixels < 20) {
        loadAddGoodList();
      }

      // print('scrollController addListener ${pixels}');
    });
  }
}
